package com.zhss.massivedata.order.controller;

import com.zhss.massivedata.order.constant.OrderStatus;
import com.zhss.massivedata.order.domain.OrderFactory;
import com.zhss.massivedata.order.domain.OrderInfo;
import com.zhss.massivedata.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 订单管理控制器
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     * @return
     */
    @GetMapping("/create")
    public String create() {
        try {
            OrderInfo orderInfo = OrderFactory.build();
            orderInfo.setOrderStatus(OrderStatus.WAIT_FOR_PAY);
            orderService.create(orderInfo);
        } catch(Exception e) {
            e.printStackTrace();
            return "failure";
        }

        return "success";
    }

    @GetMapping("/payed/{orderInfoId}")
    public String payed(@PathVariable("orderInfoId") long orderInfoId) {
        try {
            orderService.payed(orderInfoId);
        } catch(Exception e) {
            e.printStackTrace();
            return "failure";
        }
        return "success";
    }

}
